package com.om.base;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Vector;



class DBConnectionPool {
	private int checkedOut;

	private Vector freeConnections = new Vector();

	private int maxConn;

	private String name;

	private String password;

	private String URL;

	private String user;
	
	private BaseConnectionAdapter connectionAdapter;
	

	public BaseConnectionAdapter getConnectionAdapter() {
		return connectionAdapter;
	}

	public void setConnectionAdapter(BaseConnectionAdapter connectionAdapter) {
		this.connectionAdapter = connectionAdapter;
	}

	public DBConnectionPool(String name,int maxConn) {
		this.name = name;
		this.maxConn = maxConn;
	}
	
	

	public synchronized void freeConnection(Connection con) {

		try {
			freeConnections.addElement(con);
			con.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		checkedOut = freeConnections.size();
		notifyAll();

		checkedOut = freeConnections.size();
		notifyAll();
	}

	public synchronized Connection getConnection() {
		Connection con = null;
		
		if (freeConnections.size() > 0) {// 获取向量中第一个可用连接
			
			con = (Connection) freeConnections.firstElement();
			freeConnections.removeElementAt(0);
			try {
				if (con.isClosed()) {
					
					con = getConnection();
				}
			} catch (SQLException e) {
				
				con = getConnection();
			}
		} else if (maxConn == 0 || checkedOut < maxConn) {
			con = newConnection(this.name);
		}
		if (con != null) {
			checkedOut++;
			
		}
		
		return con;
	}
	
	public synchronized Connection getConnection(String name) {
		Connection con = null;
		
		if (freeConnections.size() > 0) {// 获取向量中第一个可用连接
			
			
			con = (Connection) freeConnections.firstElement();
			freeConnections.removeElementAt(0);
			try {
				if (con.isClosed()) {
					
					con = getConnection(name);
				}
			} catch (SQLException e) {
				
				con = getConnection(name);
			}
		} else if (maxConn == 0 || checkedOut < maxConn) {
			con = newConnection(name);
		}
		if (con != null) {
			checkedOut++;
			
		}
		
		return con;
	}

	
	/**
	 * 关闭所有连接
	 */
	public synchronized void release() {
		Enumeration allConnections = freeConnections.elements();
		while (allConnections.hasMoreElements()) {
			Connection con = (Connection) allConnections.nextElement();
			try {
				con.close();
				
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
		}
		freeConnections.removeAllElements();
	}

	/**
	 * 创建新的连接
	 */
	private Connection newConnection() {
		Connection con = null;
		try {
			if (user == null || "".equals(user)) {
				con = DriverManager.getConnection(URL);
			} else {
				con = DriverManager.getConnection(URL, user, password);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		return con;
	}
	
	private Connection newConnection(String name) {
		Connection con = null;
		
		try {
			this.connectionAdapter.getAvailableConnection(name);
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return con;
	}
}